package liuhao.account.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import liuhao.account.entity.AccountEntity;
import liuhao.account.service.impl.AccountServiceImpl;
import liuhao.bank.entity.BankEntity;
import liuhao.base.JsonUtil;
import liuhao.dto.PageDTO;
import liuhao.user.entity.UserEntity;
import liuhao.user.service.impl.UserServiceImpl;

@WebServlet("/AccountController.do")
public class AccountController extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	private AccountServiceImpl service=new AccountServiceImpl();
	   
	/*http://localhost:8090/liuhao/AccountController.do?method=findPage*/	 
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		  
//		  System.out.println("xx");
		  
		 String method=req.getParameter("method");
		  if("findPage".equals(method)){
			  findPage(req,resp);
		  }else if("mydel".equals(method)){
			  mydel(req,resp);
		  }else if("myadd".equals(method)){
			  myadd(req,resp);
		  }else if("myupdata".equals(method)){
			  myupdata(req,resp);
		  }else if("myquery".equals(method)){
			  myquery(req,resp);
		  }
	}

	/**
	 * 分页按钮功能
	 * @param req
	 * @param resp
	 */
	private void findPage(HttpServletRequest req, HttpServletResponse resp) {
			
		String currentPage = req.getParameter("currentPage");
		String pageSide = req.getParameter("pageSide");
		
		PageDTO page=new PageDTO();
		page.setCurrentPage(Integer.parseInt(currentPage));
		page.setPageSide(Integer.parseInt(pageSide));
		
		
//		page.setCurrentPage(1);
//		page.setPageSide(2);
		
		Map<String, Object> map = service.findAccounts(page);
		JsonUtil.WriterToJson(map, resp);
		
	}
	
	/**
	 * 删除按钮功能
	 * @param req
	 * @param resp
	 */
	private void mydel(HttpServletRequest req, HttpServletResponse resp) {
		
		String idStr = req.getParameter("idStr");
//		System.out.println();
		String[] delId = idStr.split(",");
		Map<String, Object> jg = service.deleteAccountById(delId);
		
		//以流的形式写到前台
		PrintWriter writer;
		try {
			writer = resp.getWriter();
			Gson json=new Gson();
			String jsonData = json.toJson(jg);
			writer.write(jsonData);
			System.out.println("将数据写到前端");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 查找按钮功能
	 * @param req
	 * @param resp
	 */
	private void myquery(HttpServletRequest req, HttpServletResponse resp) {
		
		String id = req.getParameter("aid");
		int fid=Integer.parseInt(id);
		AccountEntity entity=new AccountEntity();
		entity.setId(fid);
		Map<String, Object> jg = service.findById(entity);
		//以流的形式写到前台
		JsonUtil.WriterToJson(jg, resp);
	}
	
	
	/**
	 * 添加按钮功能
	 * @param req
	 * @param resp
	 */
	private void myadd(HttpServletRequest req, HttpServletResponse resp) {
		
//		String id = req.getParameter("aid");
//		int fid=Integer.parseInt(id);
		String name = req.getParameter("aname");
		String accountNo = req.getParameter("aaccountNo");
		String balance = req.getParameter("abalance");
//		String bankId = req.getParameter("bankId");
		String bankName = req.getParameter("abankName");
		
		AccountEntity entity=new AccountEntity();
//		entity.setId(fid);
		entity.setName(name);
		entity.setAccountNo(accountNo);
		entity.setBalance(Double.parseDouble(balance));
		entity.setBankName(bankName);
//		entity.setBankId(Integer.parseInt(bankId));
		Map<String, Object> jg = service.addAccountById(entity);
		
		//以流的形式写到前台
		JsonUtil.WriterToJson(jg, resp);
	}
	
	/**
	 * 修改按钮功能
	 * @param req
	 * @param resp
	 */
	private void myupdata(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("aid");
		int fid=Integer.parseInt(id);
		String name = req.getParameter("aname");
		String accountNo = req.getParameter("aaccountNo");
		String balance = req.getParameter("abalance");
		String bankName = req.getParameter("abankName");
		
		AccountEntity entity=new AccountEntity();
		entity.setId(fid);
		entity.setName(name);
		entity.setAccountNo(accountNo);
		entity.setBalance(Double.parseDouble(balance));
		entity.setBankName(bankName);
	
		Map<String, Object> jg = service.updateAccountById(entity);
		//以流的形式写到前台
		JsonUtil.WriterToJson(jg, resp);
	}
	
}
